package com.fh.controller.system.Fund;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.entity.system.Role;
import com.fh.service.system.company.CompanyService;
import com.fh.service.system.fund.FundService;
import com.fh.service.system.role.RoleService;
import com.fh.util.AppUtil;
import com.fh.util.Const;
import com.fh.util.Jurisdiction;
import com.fh.util.MD5;
import com.fh.util.ObjectExcelView;
import com.fh.util.PageData;
 
@Controller
@RequestMapping(value="/fund")
public class FundController extends BaseController {
	
	String menuUrl = "fund/listFund.do"; //菜单地址(权限用)
	@Resource(name="fundService")
	private FundService fundService;
	@Resource(name="roleService")
	private RoleService roleService;
	
	
	
	/**
	 * 保存
	 */
	@RequestMapping(value="/saveFund")
	public ModelAndView saveFund(PrintWriter out) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		
		fundService.saveFund(pd);
		mv.addObject("msg","success");
		//mv.addObject("msg","failed");
		
		mv.setViewName("save_result");
		
		return mv;
	}
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAddFund")
	public ModelAndView goAddFund(){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			mv.setViewName("system/fund/fund_edit");
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/editFund")
	public ModelAndView editFund(PrintWriter out) throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		if(pd.getString("fund_id") != null && !"".equals(pd.getString("fund_id"))){
			
			pd.put("fund_id", pd.getString("fund_id"));
		}
		if(Jurisdiction.buttonJurisdiction(menuUrl, "edit")){fundService.editFund(pd);}
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	/**
	 * 去修改页面
	 * @param fund_id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/toEditFund")
	public ModelAndView toEdit(String fund_id)throws Exception{
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			pd.put("fund_id",fund_id);
			pd = fundService.findFundById(pd);
			mv.addObject("pd", pd);
			mv.setViewName("system/fund/fund_edit2");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}

	
	/**
	 * 显示列表
	 */
	@RequestMapping(value="/listFund")
	public ModelAndView listFund(Page page){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			page.setPd(pd);
			List<PageData>	fundList = fundService.listAllFund(page);			//列出用户列表
			List<Role> roleList = roleService.listAllappERRoles();					//列出所有会员二级角色
			
			mv.setViewName("system/fund/fund_list");
			mv.addObject("fundList", fundList);
			mv.addObject("roleList", roleList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
		return mv;
	}

	/**
	 * 删除
	 */
	@RequestMapping(value="/deleteFund")
	public ModelAndView deleteFund(PrintWriter out){
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){fundService.deleteFund(pd);}
			/*out.write("success");
			out.close();*/
			mv.setViewName("system/fund/fund_list");
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 批量删除
	 */
	/*@RequestMapping(value="/deleteAllFund")
	@ResponseBody
	public Object deleteAllFund() {
		PageData pd = new PageData();
		Map<String,Object> map = new HashMap<String,Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String FUND_IDS = pd.getString("FUND_IDS");
			
			if(null != FUND_IDS && !"".equals(FUND_IDS)){
				String ArrayFUND_IDS[] = FUND_IDS.split(",");
				if(Jurisdiction.buttonJurisdiction(menuUrl, "del")){fundService.deleteAllFund(ArrayFUND_IDS);}
				pd.put("msg", "ok");
			}else{
				pd.put("msg", "no");
			}
			
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	*/
	
	
	
	
	@InitBinder
	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */
}
